package yt.kpool.activity.setting;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.adapter.PaySetAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 付款设置
 */

public class PaySetAc extends AppCompatActivity {
    @BindView(R.id.main_recycler)
    RecyclerView mRecyclerView;
    private PaySetAdapter paySetAdapter;
    private List<ChildAccountBean.DataBean> list;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payset);
        ButterKnife.bind(this);
        initTitle();
        init();
        if(NetWorkUtil.checkNet(PaySetAc.this))
        {
            getData();
        }
        else
        {

            ToastUtil.showToast(PaySetAc.this,getResources().getString(R.string.internet_error));
        }

    }
    private void init()
    {
        list=new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        paySetAdapter=new PaySetAdapter(this,list);
        mRecyclerView.setAdapter(paySetAdapter);
    }
    private void getData() {
        if (NetWorkUtil.check(this)) {


            SPUtil spUtil = new SPUtil(BaseApplication.context, BaseApplication.FILENAME);

            String id = spUtil.getString(BaseApplication.ID, "0");
            if (!"0".equals(id)) {
                loadingDialog.show();
                NetHelperNew.GetChildAccount(id, new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {
                        loadingDialog.dismiss();
                        Log.i("-->", "子账户列表：" + data);
                        ChildAccountBean childAccountBean = MyGson.getInstance().fromJson(data, ChildAccountBean.class);
                        if (childAccountBean.getType() == 1) {
                            if(childAccountBean.getData().size()>0)
                            {
                                list.clear();
                                list.addAll(childAccountBean.getData());
                                paySetAdapter.notifyDataSetChanged();
                            }
                           else
                            {
                                ToastUtil.showToast(PaySetAc.this,"没有数据");
                            }

                        }
                    }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);

                        loadingDialog.dismiss();
                        ToastUtil.showToast(getApplicationContext(), msg);
                    }
                });
            }


        }

    }
    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("付款设置");
        loadingDialog = new LoadingDialog(this, 0);
    }

}
